package com.wwwarehouse.contract.query_express;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.common.collect.Lists;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.QueryExpressBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static contract.wwwarehouse.com.contract.R.id.demanderName;
import static contract.wwwarehouse.com.contract.R.id.item1Status;
import static contract.wwwarehouse.com.contract.R.id.orderId;
import static contract.wwwarehouse.com.contract.R.id.orderTime;


/**
 * Created by clannad.shao on 2017/11/4.
 *
 * @author clannad_sk
 * @date 2017/11/4
 * 订单跟踪fragment
 */
public class QueryExpressListItemFragment extends BaseFragment implements View.OnClickListener {

    private CircleImageView mTitleImg;
    private TextView mTitleSupName;
    private TextView mDemanderName;
    private TextView mOrderId;
    private TextView mOrderTime;
    private TextView mRelatedNum;
    private TextView mSigninNum;
    private TextView mWaitrecNum;
    private LinearLayout mItem1ll;
    private ImageView mItem1Img;
    private TextView mItem1Title;
    private RelativeLayout mItem1Rl;
    private TextView mItem1Status;
    private TextView mItem1ExpName;
    private TextView mItem1Detail;
    private LinearLayout mItem2ll;
    private ImageView mItem2Img;
    private TextView mItem2Title;
    private RelativeLayout mItem2Rl;
    private TextView mItem2Status;
    private TextView mItem2ExpName;
    private TextView mItem2Detail;
    private static final int PAGE_SIZE = 2;
    private static final String WAIT_RECEIVE = "WAIT_RECEIVE";
    private static final String SIGN_IN = "SIGN_IN";
    private List<QueryExpressBean.TrackListBean> result;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.query_express_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化view
        mTitleImg = $(R.id.titleImg);
        mTitleSupName = $(R.id.titleSupName);
        mDemanderName = $(demanderName);
        mOrderId = $(orderId);
        mOrderTime = $(orderTime);
        mRelatedNum = $(R.id.relatedNum);
        mSigninNum = $(R.id.signinNum);
        mWaitrecNum = $(R.id.waitrecNum);
        mItem1ll = $(R.id.item1ll);
        mItem1Img = $(R.id.item1Img);
        mItem1Title = $(R.id.item1Title);
        mItem1Rl = $(R.id.item1Rl);
        mItem1Status = $(item1Status);
        mItem1ExpName = $(R.id.item1ExpName);
        mItem1Detail = $(R.id.item1Detail);
        mItem2ll = $(R.id.item2ll);
        mItem2Img = $(R.id.item2Img);
        mItem2Title = $(R.id.item2Title);
        mItem2Rl = $(R.id.item2Rl);
        mItem2Status = $(R.id.item2Status);
        mItem2ExpName = $(R.id.item2ExpName);
        mItem2Detail = $(R.id.item2Detail);
        mItem1ll.setOnClickListener(this);
        mItem2ll.setOnClickListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof QueryExpressListItemFragment) {
            mActivity.setTitle(getString(R.string.query_express));
        }
    }

    @Override
    public void requestDatas() {

    }


    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        initData();
        return null;
    }

    public void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            //bundle携带数据
            //需方业务单位名称
            String demanderName = bundle.getString("demanderName");
            //需方业务单位ukid
            String demanderUkid = bundle.getString("demanderUkid");
            //订单号
            String orderId = bundle.getString("orderId");
            //下单时间
            String orderTime = bundle.getString("orderTime");
            //总运单数
            int packageNum = bundle.getInt("packageNum");
            //已签收运单及待签收
            QueryExpressBean.PackageTrackNumBean packageTrackNum = bundle.getParcelable("packageTrackNum");
            //供方业务单位名称
            String supplierName = bundle.getString("supplierName");
            //供方业务单位ukid
            String supplierUkid = bundle.getString("supplierUkid");
            //供方头像
            String supplierIcon = bundle.getString("supplierIcon");
            //数据源
            ArrayList trackList = bundle.getParcelableArrayList("trackList");
            int page = bundle.getInt("page");
            mTitleSupName.setText("供方：" + (StringUtils.isNullString(supplierName) ? "" : supplierName));
            mDemanderName.setText("需方：" + (StringUtils.isNullString(demanderName) ? "" : demanderName));
            mOrderId.setText("订单号：" + (StringUtils.isNullString(orderId) ? "" : orderId));
            mOrderTime.setText("下单时间：" + (StringUtils.isNullString(orderTime) ? "" : orderTime));
            mRelatedNum.setText(String.valueOf(packageNum));
            if (StringUtils.isNullString(supplierIcon)) {
                mTitleImg.setImageResource(R.drawable.picture_no);
            } else {
                ImageloaderUtils.displayImg(supplierIcon, mTitleImg);
            }
            if (packageTrackNum != null) {
                mSigninNum.setText(String.valueOf(packageTrackNum.getSIGN_IN()));
                mWaitrecNum.setText(String.valueOf(packageTrackNum.getWAIT_RECEIVE()));
            }
            if (trackList == null || trackList.isEmpty()) {
                mItem1ll.setVisibility(View.GONE);
                mItem2ll.setVisibility(View.GONE);
                return;
            }
            List<List<QueryExpressBean.TrackListBean>> list = Lists.partition(trackList, 2);
            if (page > list.size()) {
                mItem1ll.setVisibility(View.GONE);
                mItem2ll.setVisibility(View.GONE);
                return;
            }
            result = list.get(page - 1);
            if (result.size() == PAGE_SIZE) {
                //两个item都显示
                mItem1ll.setVisibility(View.VISIBLE);
                mItem2ll.setVisibility(View.VISIBLE);
                setVule2View(result.get(0), true);
                setVule2View(result.get(1), false);
            } else if (result.size() < PAGE_SIZE) {
                //只显示一个item
                mItem1ll.setVisibility(View.VISIBLE);
                mItem2ll.setVisibility(View.GONE);
                setVule2View(result.get(0), true);
            }
        }
    }

    private void setVule2View(QueryExpressBean.TrackListBean trackBean, boolean isItem1) {
        if (StringUtils.isNullString(trackBean.getLastTrackStatus())) {
            return;
        }
        String expName = (StringUtils.isNullString(trackBean.getExpressName()) ? "" : trackBean.getExpressName())
                + "：" + (StringUtils.isNullString(trackBean.getExpressNo()) ? "" : trackBean.getExpressNo());
        String detail = StringUtils.isNullString(trackBean.getLastTrackTime()) ? "" : trackBean.getLastTrackTime()
                + " " + (StringUtils.isNullString(trackBean.getLastTrackDesc()) ? "" : trackBean.getLastTrackDesc());
        if (WAIT_RECEIVE.equals(trackBean.getLastTrackStatus()) && isItem1) {
            //待收货item1
            mItem1Title.setText("发货时间：" + trackBean.getDeliveryTime());
            mItem1ExpName.setText(expName);
            mItem1Detail.setText(detail);
            mItem1Img.setImageResource(R.drawable.qexp_circle_orange);
            mItem1Rl.setBackgroundResource(R.drawable.qexp_orange_back);
            mItem1Status.setText("待收货");
            mItem1Status.setTextColor(getResources().getColor(R.color.common_color_c2_f7a82f));
        } else if (WAIT_RECEIVE.equals(trackBean.getLastTrackStatus()) && !isItem1) {
            //待收货item2
            mItem2Title.setText("发货时间：" + trackBean.getDeliveryTime());
            mItem2ExpName.setText(expName);
            mItem2Detail.setText(detail);
            mItem2Img.setImageResource(R.drawable.qexp_circle_orange);
            mItem2Rl.setBackgroundResource(R.drawable.qexp_orange_back);
            mItem2Status.setText("待收货");
            mItem2Status.setTextColor(getResources().getColor(R.color.common_color_c2_f7a82f));
        } else if (SIGN_IN.equals(trackBean.getLastTrackStatus()) && isItem1) {
            //已签收item1
            mItem1Title.setText("发货时间：" + trackBean.getDeliveryTime());
            mItem1ExpName.setText(expName);
            mItem1Detail.setText(detail);
            mItem1Img.setImageResource(R.drawable.qexp_circle_blue);
            mItem1Rl.setBackgroundResource(R.drawable.qexp_blue_back);
            mItem1Status.setText("已签收");
            mItem1Status.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
        } else if (SIGN_IN.equals(trackBean.getLastTrackStatus()) && !isItem1) {
            //已签收item2
            mItem2Title.setText("发货时间：" + trackBean.getDeliveryTime());
            mItem2ExpName.setText(expName);
            mItem2Detail.setText(detail);
            mItem2Img.setImageResource(R.drawable.qexp_circle_blue);
            mItem2Rl.setBackgroundResource(R.drawable.qexp_blue_back);
            mItem2Status.setText("已签收");
            mItem2Status.setTextColor(getResources().getColor(R.color.common_color_c1_337cff));
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.item1ll && result != null && !result.isEmpty()) {
            pushToNext(result.get(0).getPackageUkid());
        } else if (view.getId() == R.id.item2ll && result != null && result.size() > 1) {
            pushToNext(result.get(1).getPackageUkid());
        } else {

        }
    }

    private void pushToNext(String packageUkid){
        Bundle bundle = new Bundle();
        bundle.putString("packageUkid", packageUkid);
        Fragment fragment = new ExpressDetailFragment();
        fragment.setArguments(bundle);
        pushFragment(fragment);
    }
}
